<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>十四、浅谈ES5和ES6 | HJ BLOG</title><meta name="keywords" content="JavaScript"><meta name="author" content="娄豪杰"><meta name="copyright" content="娄豪杰"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="ES5 和 ES6 我们所说的 ES5 和 ES6 其实就是在 js 语法的发展过程中的一个版本而已 比如我们使用的微信 最早的版本是没有支付功能的 随着时间的流逝，后来出现了一个版本，这个版本里面有支付功能了   ECMAScript 就是 js 的语法 以前的版本没有某些功能 在 ES5 这个版本的时候增加了一些功能 在 ES6 这个版本的时候增加了一些功能   因为浏览器是浏览器厂商生产的">
<meta property="og:type" content="article">
<meta property="og:title" content="十四、浅谈ES5和ES6">
<meta property="og:url" content="http://example.com/article/b77b8aef.html">
<meta property="og:site_name" content="HJ BLOG">
<meta property="og:description" content="ES5 和 ES6 我们所说的 ES5 和 ES6 其实就是在 js 语法的发展过程中的一个版本而已 比如我们使用的微信 最早的版本是没有支付功能的 随着时间的流逝，后来出现了一个版本，这个版本里面有支付功能了   ECMAScript 就是 js 的语法 以前的版本没有某些功能 在 ES5 这个版本的时候增加了一些功能 在 ES6 这个版本的时候增加了一些功能   因为浏览器是浏览器厂商生产的">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645530798&t=e5c6ac493d1c8b04cd2a4740e1cc9870">
<meta property="article:published_time" content="2019-03-13T16:00:00.000Z">
<meta property="article:modified_time" content="2022-01-23T11:55:35.540Z">
<meta property="article:author" content="娄豪杰">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645530798&t=e5c6ac493d1c8b04cd2a4740e1cc9870"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="http://example.com/article/b77b8aef"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '十四、浅谈ES5和ES6',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-01-23 19:55:35'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/static-butterfly/dist/css/index.min.css"><link rel="stylesheet" href="/css/custom.css"><meta name="generator" content="Hexo 5.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">70</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">17</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa-fw fa fa-book"></i><span> 阅读</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa-fw fa fa-bars"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/photo"><i class="fa-fw fas fa-image"></i><span> 相册</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa-fw fas fa-heart card-announcement-animation"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&amp;refer=http%3A%2F%2Fpic1.zhimg.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1645530798&amp;t=e5c6ac493d1c8b04cd2a4740e1cc9870')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">HJ BLOG</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa-fw fa fa-book"></i><span> 阅读</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fa-fw fa fa-bars"></i><span> 娱乐</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/photo"><i class="fa-fw fas fa-image"></i><span> 相册</span></a></li><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> 电影</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa-fw fas fa-heart card-announcement-animation"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">十四、浅谈ES5和ES6</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2019-03-13T16:00:00.000Z" title="发表于 2019-03-14 00:00:00">2019-03-14</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-01-23T11:55:35.540Z" title="更新于 2022-01-23 19:55:35">2022-01-23</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/JavaScript/">JavaScript</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="十四、浅谈ES5和ES6"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="ES5-和-ES6"><a href="#ES5-和-ES6" class="headerlink" title="ES5 和 ES6"></a>ES5 和 ES6</h1><ul>
<li>我们所说的 ES5 和 ES6 其实就是在 js 语法的发展过程中的一个版本而已</li>
<li>比如我们使用的微信<ul>
<li>最早的版本是没有支付功能的</li>
<li>随着时间的流逝，后来出现了一个版本，这个版本里面有支付功能了</li>
</ul>
</li>
<li>ECMAScript 就是 js 的语法<ul>
<li>以前的版本没有某些功能</li>
<li>在 ES5 这个版本的时候增加了一些功能</li>
<li>在 ES6 这个版本的时候增加了一些功能</li>
</ul>
</li>
<li>因为浏览器是浏览器厂商生产的<ul>
<li>ECMAScript 发布了新的功能以后，浏览器厂商需要让自己的浏览器支持这些功能</li>
<li>这个过程是需要时间的</li>
<li>所以到现在，基本上大部分浏览器都可以比较完善的支持了</li>
<li>只不过有些浏览器还是不能全部支持</li>
<li>这就出现了兼容性问题</li>
<li>所以我们写代码的时候就要考虑哪些方法是 ES5 或者 ES6 的，看看是不是浏览器都支持</li>
</ul>
</li>
</ul>
<h2 id="ES5-增加的数组常用方法"><a href="#ES5-增加的数组常用方法" class="headerlink" title="ES5 增加的数组常用方法"></a>ES5 增加的数组常用方法</h2><h3 id="数组方法之-forEach"><a href="#数组方法之-forEach" class="headerlink" title="数组方法之 forEach"></a>数组方法之 forEach</h3><ul>
<li><p><code>forEach</code> 用于遍历数组，和 for 循环遍历数组一个道理</p>
</li>
<li><p>语法： <code>数组.forEach(function (item, index, arr) &#123;&#125;)</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>];</span><br><span class="line"><span class="comment">// forEach 就是将数组循环遍历，数组中有多少项，那么这个函数就执行多少回</span></span><br><span class="line">arr.forEach(<span class="function"><span class="keyword">function</span> (<span class="params">item, index, arr</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 在这个函数内部</span></span><br><span class="line">  <span class="comment">// item 就是数组中的每一项</span></span><br><span class="line">  <span class="comment">// index 就是每一项对应的索引</span></span><br><span class="line">  <span class="comment">// arr 就是原始数组</span></span><br><span class="line">  <span class="built_in">console</span>.log(item);</span><br><span class="line">  <span class="built_in">console</span>.log(index);</span><br><span class="line">  <span class="built_in">console</span>.log(arr);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<ul>
<li>上面的代码就等价于</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>];</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">  fn(arr[i], i, arr);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">item, index, arr</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(item);</span><br><span class="line">  <span class="built_in">console</span>.log(index);</span><br><span class="line">  <span class="built_in">console</span>.log(arr);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="数组方法之-map"><a href="#数组方法之-map" class="headerlink" title="数组方法之 map"></a>数组方法之 map</h3></li>
<li><p><code>map</code> 用于遍历数组，和 forEach 基本一致，只不过是有一个返回值</p>
</li>
<li><p>语法： <code>数组.map(function (item, index, arr) &#123;&#125;)</code></p>
</li>
<li><p>返回值： 一个新的数组</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>];</span><br><span class="line"><span class="comment">// forEach 就是将数组循环遍历，数组中有多少项，那么这个函数就执行多少回</span></span><br><span class="line"><span class="keyword">var</span> newArr = arr.map(<span class="function"><span class="keyword">function</span> (<span class="params">item, index, arr</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 函数里面的三个参数和 forEach 一样</span></span><br><span class="line">  <span class="comment">// 我们可以在这里操作数组中的每一项，</span></span><br><span class="line">  <span class="comment">// return 操作后的每一项</span></span><br><span class="line">  <span class="keyword">return</span> item + <span class="string">&quot;11&quot;</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">console</span>.log(newArr); <span class="comment">// [&quot;a11&quot;, &quot;b11&quot;, &quot;c11&quot;]</span></span><br></pre></td></tr></table></figure>
<ul>
<li>返回值就是我们每次对数组的操作</li>
<li>等价于</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="string">&quot;a&quot;</span>, <span class="string">&quot;b&quot;</span>, <span class="string">&quot;c&quot;</span>];</span><br><span class="line"><span class="keyword">var</span> newArr = [];</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">  newArr.push(fn(arr[i], i, arr));</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">item, index, arr</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> item + <span class="string">&quot;11&quot;</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(newArr);</span><br></pre></td></tr></table></figure>
<h3 id="数组方法之-filter"><a href="#数组方法之-filter" class="headerlink" title="数组方法之 filter"></a>数组方法之 filter</h3></li>
<li><p><code>filter</code> ： 是将数组遍历一遍，按照我们的要求把数数组中符合的内容过滤出来</p>
</li>
<li><p>语法： <code>数组.filter(function (item, index, arr) &#123;&#125;)</code></p>
</li>
<li><p>返回值： 根据我们的条件过滤出来的新数组</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">var</span> newArr = arr.filter(<span class="function"><span class="keyword">function</span> (<span class="params">item, index, arr</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 函数内部的三个参数和 forEach 一样</span></span><br><span class="line">  <span class="comment">// 我们把我们的条件 return 出去</span></span><br><span class="line">  <span class="keyword">return</span> item &gt; <span class="number">2</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">console</span>.log(newArr); <span class="comment">// [3, 4, 5]</span></span><br></pre></td></tr></table></figure>
<ul>
<li>新数组里面全都是大于 2 的数字</li>
<li>等价于</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">var</span> newArr = [];</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">  <span class="keyword">if</span> (fn(arr[i], i, arr)) &#123;</span><br><span class="line">    newArr.push(arr[i]);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">item, index, arr</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> item &gt; <span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(newArr);</span><br></pre></td></tr></table></figure>
<h3 id="数组方法之-some"><a href="#数组方法之-some" class="headerlink" title="数组方法之 some"></a>数组方法之 some</h3></li>
<li><p><code>some</code> ： 是将数组遍历一遍，按照我们的要求判断一下数组中是否有一些元素满足条件</p>
</li>
<li><p>语法： <code>数组.some(function (item, index, arr) &#123;&#125;)</code></p>
</li>
<li><p>返回值： 有一些满足条件返回 true，反之 false</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">var</span> flag = arr.some(<span class="function"><span class="keyword">function</span> (<span class="params">item, index, arr</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 函数内部的三个参数和 forEach 一样</span></span><br><span class="line">  <span class="comment">// 我们把我们的条件 return 出去</span></span><br><span class="line">  <span class="keyword">return</span> item &gt; <span class="number">2</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">console</span>.log(flag); <span class="comment">// true</span></span><br></pre></td></tr></table></figure>
<h3 id="数组方法之-every"><a href="#数组方法之-every" class="headerlink" title="数组方法之 every"></a>数组方法之 every</h3></li>
<li><p><code>every</code> ： 是将数组遍历一遍，按照我们的要求判断一下数组中是否所有元素满足条件</p>
</li>
<li><p>语法： <code>数组.every(function (item, index, arr) &#123;&#125;)</code></p>
</li>
<li><p>返回值： 全都都满足条件返回 true，反之 false</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">var</span> flag = arr.every(<span class="function"><span class="keyword">function</span> (<span class="params">item, index, arr</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 函数内部的三个参数和 forEach 一样</span></span><br><span class="line">  <span class="comment">// 我们把我们的条件 return 出去</span></span><br><span class="line">  <span class="keyword">return</span> item &gt; <span class="number">2</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">console</span>.log(flag); <span class="comment">// false</span></span><br></pre></td></tr></table></figure>
<h2 id="ES7-增加的数组常用方法"><a href="#ES7-增加的数组常用方法" class="headerlink" title="ES7 增加的数组常用方法"></a>ES7 增加的数组常用方法</h2></li>
</ul>
<h3 id="数组方法之-find"><a href="#数组方法之-find" class="headerlink" title="数组方法之 find"></a>数组方法之 find</h3><ul>
<li><p><code>find</code> ： 是将数组遍历一遍，按照我们的要求查找数组中满足条件的第一个元素</p>
</li>
<li><p>语法： <code>数组.find(function (item, index, arr) &#123;&#125;)</code></p>
</li>
<li><p>返回值： 返回满足条件的第一个元素</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">var</span> res = arr.find(<span class="function"><span class="keyword">function</span> (<span class="params">item, index, arr</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 函数内部的三个参数和 forEach 一样</span></span><br><span class="line">  <span class="comment">// 我们把我们的条件 return 出去</span></span><br><span class="line">  <span class="keyword">return</span> item == <span class="number">2</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">console</span>.log(res); <span class="comment">// 2</span></span><br></pre></td></tr></table></figure>
<h3 id="数组方法之-findIndex"><a href="#数组方法之-findIndex" class="headerlink" title="数组方法之 findIndex"></a>数组方法之 findIndex</h3></li>
<li><p><code>findIndex</code> ： 是将数组遍历一遍，按照我们的要求查找数组中满足条件的元素</p>
</li>
<li><p>语法： <code>数组.findIndex(function (item, index, arr) &#123;&#125;)</code></p>
</li>
<li><p>返回值： 返回满足条件的第一个元素的下标</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">var</span> res = arr.findIndex(<span class="function"><span class="keyword">function</span> (<span class="params">item, index, arr</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// 函数内部的三个参数和 forEach 一样</span></span><br><span class="line">  <span class="comment">// 我们把我们的条件 return 出去</span></span><br><span class="line">  <span class="keyword">return</span> item == <span class="number">2</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">console</span>.log(res); <span class="comment">// 1</span></span><br></pre></td></tr></table></figure>
<h2 id="JSON-方法"><a href="#JSON-方法" class="headerlink" title="JSON 方法"></a>JSON 方法</h2></li>
<li><p><code>json</code> 是一种特殊的字符串格式，本质是一个字符串</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> jsonObj = <span class="string">&#x27;&#123; &quot;name&quot;: &quot;Jack&quot;, &quot;age&quot;: 18, &quot;gender&quot;: &quot;男&quot; &#125;&#x27;</span>;</span><br><span class="line"><span class="keyword">var</span> jsonArr =</span><br><span class="line">  <span class="string">&#x27;[&#123; &quot;name&quot;: &quot;Jack&quot;, &quot;age&quot;: 18, &quot;gender&quot;: &quot;男&quot; &#125;, &#123; &quot;name&quot;: &quot;Jack&quot;, &quot;age&quot;: 18, &quot;gender&quot;: &quot;男&quot; &#125;, &#123; &quot;name&quot;: &quot;Jack&quot;, &quot;age&quot;: 18, &quot;gender&quot;: &quot;男&quot; &#125;]&#x27;</span>;</span><br></pre></td></tr></table></figure></li>
<li><p>就是对象内部的 <code>key</code> 和 <code>value</code> 都用双引号包裹的字符串（必须是双引号）</p>
</li>
</ul>
<h2 id="JSON-的两个方法（es5-新增）"><a href="#JSON-的两个方法（es5-新增）" class="headerlink" title="JSON 的两个方法（es5 新增）"></a>JSON 的两个方法（es5 新增）</h2><ul>
<li>我们有两个方法可以使用 <strong>JSON.parse</strong></li>
<li></li>
<li><code>json.stringify</code> 是将 js 的对象或者数组转换成为 json 格式的字符串</li>
</ul>
<h3 id="JSON-parse"><a href="#JSON-parse" class="headerlink" title="JSON.parse"></a>JSON.parse</h3><ul>
<li><p><code>JSON.parse</code> 是将 json 格式的字符串转换为 js 的对象或者数组</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> jsonObj = <span class="string">&#x27;&#123; &quot;name&quot;: &quot;Jack&quot;, &quot;age&quot;: 18, &quot;gender&quot;: &quot;男&quot; &#125;&#x27;</span>;</span><br><span class="line"><span class="keyword">var</span> jsonArr =</span><br><span class="line">  <span class="string">&#x27;[&#123; &quot;name&quot;: &quot;Jack&quot;, &quot;age&quot;: 18, &quot;gender&quot;: &quot;男&quot; &#125;, &#123; &quot;name&quot;: &quot;Jack&quot;, &quot;age&quot;: 18, &quot;gender&quot;: &quot;男&quot; &#125;, &#123; &quot;name&quot;: &quot;Jack&quot;, &quot;age&quot;: 18, &quot;gender&quot;: &quot;男&quot; &#125;]&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> obj = <span class="built_in">JSON</span>.parse(jsonStr);</span><br><span class="line"><span class="keyword">var</span> arr = <span class="built_in">JSON</span>.parse(jsonArr);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(obj);</span><br><span class="line"><span class="built_in">console</span>.log(arr);</span><br></pre></td></tr></table></figure>
<ul>
<li><code>obj</code> 就是我们 js 的对象</li>
<li><code>arr</code> 就是我们 js 的数组</li>
</ul>
</li>
</ul>
<h3 id="JSON-stringify"><a href="#JSON-stringify" class="headerlink" title="JSON.stringify"></a>JSON.stringify</h3><ul>
<li><p><code>JSON.parse</code> 是将 json 格式的字符串转换为 js 的对象或者数组</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">  name: <span class="string">&quot;Jack&quot;</span>,</span><br><span class="line">  age: <span class="number">18</span>,</span><br><span class="line">  gender: <span class="string">&quot;男&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">var</span> arr = [</span><br><span class="line">  &#123;</span><br><span class="line">    name: <span class="string">&quot;Jack&quot;</span>,</span><br><span class="line">    age: <span class="number">18</span>,</span><br><span class="line">    gender: <span class="string">&quot;男&quot;</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    name: <span class="string">&quot;Jack&quot;</span>,</span><br><span class="line">    age: <span class="number">18</span>,</span><br><span class="line">    gender: <span class="string">&quot;男&quot;</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    name: <span class="string">&quot;Jack&quot;</span>,</span><br><span class="line">    age: <span class="number">18</span>,</span><br><span class="line">    gender: <span class="string">&quot;男&quot;</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">];</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> jsonObj = <span class="built_in">JSON</span>.stringify(obj);</span><br><span class="line"><span class="keyword">var</span> jsonArr = <span class="built_in">JSON</span>.stringify(arr);</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(jsonObj);</span><br><span class="line"><span class="built_in">console</span>.log(jsonArr);</span><br></pre></td></tr></table></figure>
<ul>
<li><code>jsonObj</code> 就是 json 格式的对象字符串</li>
<li><code>jsonArr</code> 就是 json 格式的数组字符串</li>
</ul>
</li>
</ul>
<h2 id="this-关键字"><a href="#this-关键字" class="headerlink" title="this 关键字"></a>this 关键字</h2><ul>
<li><p>每一个函数内部都有一个关键字是 <code>this</code></p>
</li>
<li><p>可以让我们直接使用的</p>
</li>
<li><p>重点： <strong>函数内部的 this 只和函数的调用方式有关系，和函数的定义方式没有关系</strong></p>
</li>
<li><p>函数内部的 this 指向谁，取决于函数的调用方式</p>
<ul>
<li><p>全局定义的函数直接调用，<code>this =&gt; window</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">this</span>);</span><br><span class="line">&#125;</span><br><span class="line">fn();</span><br><span class="line"><span class="comment">// 此时 this 指向 window</span></span><br></pre></td></tr></table></figure></li>
<li><p>对象内部的方法调用，<code>this =&gt; 调用者</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">  fn: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">this</span>);</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line">obj.fn();</span><br><span class="line"><span class="comment">// 此时 this 指向 obj</span></span><br></pre></td></tr></table></figure></li>
<li><p>定时器的处理函数，<code>this =&gt; window</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">setTimeout</span>(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">this</span>);</span><br><span class="line">&#125;, <span class="number">0</span>);</span><br><span class="line"><span class="comment">// 此时定时器处理函数里面的 this 指向 window</span></span><br></pre></td></tr></table></figure></li>
<li><p>事件处理函数，<code>this =&gt; 事件源</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">div.onclick = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">this</span>);</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 当你点击 div 的时候，this 指向 div</span></span><br></pre></td></tr></table></figure></li>
<li><p>自调用函数，<code>this =&gt; window</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">this</span>);</span><br><span class="line">&#125;)();</span><br><span class="line"><span class="comment">// 此时 this 指向 window</span></span><br></pre></td></tr></table></figure>
<h3 id="call-和-apply-和-bind"><a href="#call-和-apply-和-bind" class="headerlink" title="call 和 apply 和 bind"></a>call 和 apply 和 bind</h3></li>
</ul>
</li>
<li><p>刚才我们说过的都是函数的基本调用方式里面的 this 指向</p>
</li>
<li><p>我们还有三个可以忽略函数本身的 this 指向转而指向别的地方</p>
</li>
<li><p>这三个方法就是 <strong>call</strong> / <strong>apply</strong> / <strong>bind</strong></p>
</li>
<li><p>是强行改变 this 指向的方法</p>
</li>
</ul>
<h4 id="call"><a href="#call" class="headerlink" title="call"></a>call</h4><ul>
<li><p><code>call</code> 方法是附加在函数调用后面使用，可以忽略函数本身的 this 指向</p>
</li>
<li><p>语法： <code>函数名.call(要改变的 this 指向，要给函数传递的参数1，要给函数传递的参数2， ...)</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123; <span class="attr">name</span>: <span class="string">&quot;Jack&quot;</span> &#125;;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">this</span>);</span><br><span class="line">  <span class="built_in">console</span>.log(a);</span><br><span class="line">  <span class="built_in">console</span>.log(b);</span><br><span class="line">&#125;</span><br><span class="line">fn(<span class="number">1</span>, <span class="number">2</span>);</span><br><span class="line">fn.call(obj, <span class="number">1</span>, <span class="number">2</span>);</span><br></pre></td></tr></table></figure>
<ul>
<li><code>fn()</code> 的时候，函数内部的 this 指向 window</li>
<li><code>fn.call(obj, 1, 2)</code> 的时候，函数内部的 this 就指向了 obj 这个对象</li>
<li>使用 call 方法的时候<ul>
<li>会立即执行函数</li>
<li>第一个参数是你要改变的函数内部的 this 指向</li>
<li>第二个参数开始，依次是向函数传递参数</li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 id="apply"><a href="#apply" class="headerlink" title="apply"></a>apply</h4><ul>
<li><p><code>apply</code> 方法是附加在函数调用后面使用，可以忽略函数本身的 this 指向</p>
</li>
<li><p>语法： <code>函数名.apply(要改变的 this 指向，[要给函数传递的参数1， 要给函数传递的参数2， ...])</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123; <span class="attr">name</span>: <span class="string">&quot;Jack&quot;</span> &#125;;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">this</span>);</span><br><span class="line">  <span class="built_in">console</span>.log(a);</span><br><span class="line">  <span class="built_in">console</span>.log(b);</span><br><span class="line">&#125;</span><br><span class="line">fn(<span class="number">1</span>, <span class="number">2</span>);</span><br><span class="line">fn.call(obj, [<span class="number">1</span>, <span class="number">2</span>]);</span><br></pre></td></tr></table></figure>
<ul>
<li><code>fn()</code> 的时候，函数内部的 this 指向 window</li>
<li><code>fn.apply(obj, [1, 2])</code> 的时候，函数内部的 this 就指向了 obj 这个对象</li>
<li>使用 apply 方法的时候<ul>
<li>会立即执行函数</li>
<li>第一个参数是你要改变的函数内部的 this 指向</li>
<li>第二个参数是一个 <strong>数组</strong>，数组里面的每一项依次是向函数传递的参数</li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 id="bind"><a href="#bind" class="headerlink" title="bind"></a>bind</h4><ul>
<li><p><code>bind</code> 方法是附加在函数调用后面使用，可以忽略函数本身的 this 指向</p>
</li>
<li><p>和 call / apply 有一些不一样，就是不会立即执行函数，而是返回一个已经改变了 this 指向的函数</p>
</li>
<li><p>语法： <code>var newFn = 函数名.bind(要改变的 this 指向); newFn(传递参数)</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123; <span class="attr">name</span>: <span class="string">&quot;Jack&quot;</span> &#125;;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="built_in">this</span>);</span><br><span class="line">  <span class="built_in">console</span>.log(a);</span><br><span class="line">  <span class="built_in">console</span>.log(b);</span><br><span class="line">&#125;</span><br><span class="line">fn(<span class="number">1</span>, <span class="number">2</span>);</span><br><span class="line"><span class="keyword">var</span> newFn = fn.bind(obj);</span><br><span class="line">newFn(<span class="number">1</span>, <span class="number">2</span>);</span><br></pre></td></tr></table></figure>
<ul>
<li>bind 调用的时候，不会执行 fn 这个函数，而是返回一个新的函数</li>
<li>这个新的函数就是一个改变了 this 指向以后的 fn 函数</li>
<li><code>fn(1, 2)</code> 的时候 this 指向 window</li>
<li><code>newFn(1, 2)</code> 的时候执行的是一个和 fn 一摸一样的函数，只不过里面的 this 指向改成了 obj</li>
</ul>
</li>
</ul>
<h2 id="ES6-新增的内容"><a href="#ES6-新增的内容" class="headerlink" title="ES6 新增的内容"></a>ES6 新增的内容</h2><ul>
<li>之前的都是 大部分是 ES5 的内容</li>
<li>接下来我们聊一下 ES6 的内容</li>
</ul>
<h3 id="新增数据类型"><a href="#新增数据类型" class="headerlink" title="新增数据类型"></a>新增数据类型</h3><p>symbol 类型-表示独一无二的值</p>
<p>symbol 函数：参数 1 用来描述 symbol</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">let s1 &#x3D; symbol(&#39;a&#39;)</span><br><span class="line">let s2 &#x3D; symbol(&#39;a&#39;)</span><br><span class="line">console.log(s1&#x3D;&#x3D;&#x3D;s2)&#x2F;&#x2F;false</span><br><span class="line"></span><br><span class="line">&#x2F;&#x2F;作为对象的属性名去使用防止 新增属性名的时候老的属性被覆盖</span><br><span class="line"></span><br><span class="line">let obj &#x3D; &#123;name:&quot;zhangsan&quot;,age:18&#125;;</span><br><span class="line">let age &#x3D; symbol(&#39;age&#39;);</span><br><span class="line">obj[age] &#x3D; 19;&#x2F;&#x2F;新增属性age，不会覆盖原来的age属性</span><br><span class="line"></span><br><span class="line">obj[age]&#x2F;&#x2F;获取symbol属性age     19</span><br><span class="line">obj[&#39;age&#39;]&#x2F;&#x2F;获取字符串属性age    18</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="新增数据结构-set-和-map"><a href="#新增数据结构-set-和-map" class="headerlink" title="新增数据结构 set 和 map"></a>新增数据结构 set 和 map</h3><p>set 类似数组，可以不会存放重复元素</p>
<p>参数 1：数组。也可以不传</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">let s1 &#x3D; new Set([a,b,a]);</span><br><span class="line">console.log(s1)&#x2F;&#x2F;a,b 会把重复的元素去掉</span><br><span class="line">        &#x2F;&#x2F; 遍历</span><br><span class="line">        s1.forEach(function(v)&#123;</span><br><span class="line">            console.log(v)&#x2F;&#x2F;遍历set中的元素</span><br><span class="line">        &#125;)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> &#x2F;&#x2F;数组去重 把arr中的重复元素去掉，得到一个新数组</span><br><span class="line">let arr &#x3D; [1,2,3,1,2,3]</span><br><span class="line">let resArr &#x3D; [...new Set(arr)]</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>map 类似对象，可以存放键值对，属性名：属性值， map 的属性名可以是任何类型的，而普通对象的属性名只能是字符串</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">let obj &#x3D; [</span><br><span class="line">            [&#39;username&#39;,&quot;aaa&quot;],</span><br><span class="line">            [&#39;age&#39;,19]</span><br><span class="line">        ]</span><br><span class="line">        &#x2F;&#x2F; 创建</span><br><span class="line">        let m &#x3D; new Map(obj)</span><br><span class="line">        &#x2F;&#x2F; 设置键值对</span><br><span class="line">        m.set(&#39;sex&#39;,&#39;男&#39;);</span><br><span class="line">        m.set(true,&#39;真&#39;)</span><br><span class="line">        &#x2F;&#x2F; 根据键 获取 值</span><br><span class="line">        m.get(&#39;sex&#39;)</span><br><span class="line">        m.get(true)</span><br></pre></td></tr></table></figure>
<h3 id="let-和-const-关键字-l"><a href="#let-和-const-关键字-l" class="headerlink" title="let 和 const 关键字 l"></a>let 和 const 关键字 l</h3><ul>
<li><p>我们以前都是使用 <code>var</code> 关键字来声明变量的</p>
</li>
<li><p>在 ES6 的时候，多了两个关键字 <code>let</code> 和 <code>const</code>，也是用来声明变量的</p>
</li>
<li><p>只不过和 var 有一些区别</p>
<ol>
<li> <code>let</code> 和 <code>const</code> 不允许重复声明变量</li>
</ol>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用 var 的时候重复声明变量是没问题的，只不过就是后面会把前面覆盖掉</span></span><br><span class="line"><span class="keyword">var</span> num = <span class="number">100</span>;</span><br><span class="line"><span class="keyword">var</span> num = <span class="number">200</span>;</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用 let 重复声明变量的时候就会报错了</span></span><br><span class="line"><span class="keyword">let</span> num = <span class="number">100</span>;</span><br><span class="line"><span class="keyword">let</span> num = <span class="number">200</span>; <span class="comment">// 这里就会报错了</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用 const 重复声明变量的时候就会报错</span></span><br><span class="line"><span class="keyword">const</span> num = <span class="number">100</span>;</span><br><span class="line"><span class="keyword">const</span> num = <span class="number">200</span>; <span class="comment">// 这里就会报错了</span></span><br></pre></td></tr></table></figure>
<ol start="2">
<li><p><code>let</code> 和 <code>const</code> 声明的变量不会在预解析的时候解析（也就是没有变量提升）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 因为预解析（变量提升）的原因，在前面是有这个变量的，只不过没有赋值</span></span><br><span class="line"><span class="built_in">console</span>.log(num); <span class="comment">// undefined</span></span><br><span class="line"><span class="keyword">var</span> num = <span class="number">100</span>;</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 因为 let 不会进行预解析（变量提升），所以直接报错了</span></span><br><span class="line"><span class="built_in">console</span>.log(num); <span class="comment">// undefined</span></span><br><span class="line"><span class="keyword">let</span> num = <span class="number">100</span>;</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 因为 const 不会进行预解析（变量提升），所以直接报错了</span></span><br><span class="line"><span class="built_in">console</span>.log(num); <span class="comment">// undefined</span></span><br><span class="line"><span class="keyword">const</span> num = <span class="number">100</span>;</span><br></pre></td></tr></table></figure></li>
<li><p><code>let</code> 和 <code>const</code> 声明的变量会被所有代码块限制作用范围</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// var 声明的变量只有函数能限制其作用域，其他的不能限制</span></span><br><span class="line"><span class="keyword">if</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">  <span class="keyword">var</span> num = <span class="number">100</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(num); <span class="comment">// 100</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// let 声明的变量，除了函数可以限制，所有的代码块都可以限制其作用域（if/while/for/...）</span></span><br><span class="line"><span class="keyword">if</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">  <span class="keyword">let</span> num = <span class="number">100</span>;</span><br><span class="line">  <span class="built_in">console</span>.log(num); <span class="comment">// 100</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(num); <span class="comment">// 报错</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// const 声明的变量，除了函数可以限制，所有的代码块都可以限制其作用域（if/while/for/...）</span></span><br><span class="line"><span class="keyword">if</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">  <span class="keyword">const</span> num = <span class="number">100</span>;</span><br><span class="line">  <span class="built_in">console</span>.log(num); <span class="comment">// 100</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(num); <span class="comment">// 报错</span></span><br></pre></td></tr></table></figure></li>
</ol>
</li>
<li><p><code>let</code> 和 <code>const</code> 的区别</p>
<ol>
<li><p><code>let</code> 声明的变量的值可以改变，<code>const</code> 声明的变量的值不可以改变</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> num = <span class="number">100</span>;</span><br><span class="line">num = <span class="number">200</span>;</span><br><span class="line"><span class="built_in">console</span>.log(num); <span class="comment">// 200</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> num = <span class="number">100</span>;</span><br><span class="line">num = <span class="number">200</span>; <span class="comment">// 这里就会报错了，因为 const 声明的变量值不可以改变（我们也叫做常量）</span></span><br></pre></td></tr></table></figure></li>
<li><p><code>let</code> 声明的时候可以不赋值，<code>const</code> 声明的时候必须赋值</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> num;</span><br><span class="line">num = <span class="number">100</span>;</span><br><span class="line"><span class="built_in">console</span>.log(num); <span class="comment">// 100</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> num; <span class="comment">// 这里就会报错了，因为 const 声明的时候必须赋值</span></span><br></pre></td></tr></table></figure>
<h3 id="箭头函数"><a href="#箭头函数" class="headerlink" title="箭头函数"></a>箭头函数</h3></li>
</ol>
</li>
<li><p>箭头函数是 ES6 里面一个简写函数的语法方式</p>
</li>
<li><p>重点： <strong>箭头函数只能简写函数表达式，不能简写声明式函数</strong></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params"></span>) </span>&#123;&#125; <span class="comment">// 不能简写</span></span><br><span class="line"><span class="keyword">const</span> fun = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;&#125;; <span class="comment">// 可以简写</span></span><br><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  fn: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;&#125;, <span class="comment">// 可以简写</span></span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></li>
<li><p>语法： <code>(函数的行参) =&gt; &#123; 函数体内要执行的代码 &#125;</code></p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fn = <span class="function"><span class="keyword">function</span> (<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(a);</span><br><span class="line">  <span class="built_in">console</span>.log(b);</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 可以使用箭头函数写成</span></span><br><span class="line"><span class="keyword">const</span> fun = <span class="function">(<span class="params">a, b</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(a);</span><br><span class="line">  <span class="built_in">console</span>.log(b);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  fn: <span class="function"><span class="keyword">function</span> (<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(a);</span><br><span class="line">    <span class="built_in">console</span>.log(b);</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 可以使用箭头函数写成</span></span><br><span class="line"><span class="keyword">const</span> obj2 = &#123;</span><br><span class="line">  fn: <span class="function">(<span class="params">a, b</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(a);</span><br><span class="line">    <span class="built_in">console</span>.log(b);</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h4 id="箭头函数的特殊性"><a href="#箭头函数的特殊性" class="headerlink" title="箭头函数的特殊性"></a>箭头函数的特殊性</h4></li>
<li><p>箭头函数内部没有 this，箭头函数的 this 是上下文的 this</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 在箭头函数定义的位置往上数，这一行是可以打印出 this 的</span></span><br><span class="line"><span class="comment">// 因为这里的 this 是 window</span></span><br><span class="line"><span class="comment">// 所以箭头函数内部的 this 就是 window</span></span><br><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  fn: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">this</span>);</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">// 这个位置是箭头函数的上一行，但是不能打印出 this</span></span><br><span class="line">  fun: <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 箭头函数内部的 this 是书写箭头函数的上一行一个可以打印出 this 的位置</span></span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">this</span>);</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">obj.fn();</span><br><span class="line">obj.fun();</span><br></pre></td></tr></table></figure>
<ul>
<li>按照我们之前的 this 指向来判断，两个都应该指向 obj</li>
<li>但是 fun 因为是箭头函数，所以 this 不指向 obj，而是指向 fun 的外层，就是 window</li>
</ul>
</li>
<li><p>箭头函数内部没有 <code>arguments</code> 这个参数集合</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  fn: <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">arguments</span>);</span><br><span class="line">  &#125;,</span><br><span class="line">  fun: <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">arguments</span>);</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line">obj.fn(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>); <span class="comment">// 会打印一个伪数组 [1, 2, 3]</span></span><br><span class="line">obj.fun(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>); <span class="comment">// 会直接报错</span></span><br></pre></td></tr></table></figure></li>
<li><p>函数的行参只有一个的时候可以不写 <code>()</code> 其余情况必须写</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  fn: <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;没有参数，必须写小括号&quot;</span>);</span><br><span class="line">  &#125;,</span><br><span class="line">  fn2: <span class="function">(<span class="params">a</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;一个行参，可以不写小括号&quot;</span>);</span><br><span class="line">  &#125;,</span><br><span class="line">  fn3: <span class="function">(<span class="params">a, b</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;两个或两个以上参数，必须写小括号&quot;</span>);</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure></li>
<li><p>函数体只有一行代码的时候，可以不写 <code>&#123;&#125;</code> ，并且会自动 return</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  fn: <span class="function">(<span class="params">a</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> a + <span class="number">10</span>;</span><br><span class="line">  &#125;,</span><br><span class="line">  fun: <span class="function">(<span class="params">a</span>) =&gt;</span> a + <span class="number">10</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(fn(<span class="number">10</span>)); <span class="comment">// 20</span></span><br><span class="line"><span class="built_in">console</span>.log(fun(<span class="number">10</span>)); <span class="comment">// 20</span></span><br></pre></td></tr></table></figure>
<h3 id="函数传递参数的时候的默认值"><a href="#函数传递参数的时候的默认值" class="headerlink" title="函数传递参数的时候的默认值"></a>函数传递参数的时候的默认值</h3></li>
<li><p>我们在定义函数的时候，有的时候需要一个默认值出现</p>
</li>
<li><p>就是当我不传递参数的时候，使用默认值，传递参数了就使用传递的参数</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">a</span>) </span>&#123;</span><br><span class="line">  a = a || <span class="number">10</span>;</span><br><span class="line">  <span class="built_in">console</span>.log(a);</span><br><span class="line">&#125;</span><br><span class="line">fn(); <span class="comment">// 不传递参数的时候，函数内部的 a 就是 10</span></span><br><span class="line">fn(<span class="number">20</span>); <span class="comment">// 传递了参数 20 的时候，函数内部的 a 就是 20</span></span><br></pre></td></tr></table></figure>
<ul>
<li>在 ES6 中我们可以直接把默认值写在函数的行参位置</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">a = <span class="number">10</span></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(a);</span><br><span class="line">&#125;</span><br><span class="line">fn(); <span class="comment">// 不传递参数的时候，函数内部的 a 就是 10</span></span><br><span class="line">fn(<span class="number">20</span>); <span class="comment">// 传递了参数 20 的时候，函数内部的 a 就是 20</span></span><br></pre></td></tr></table></figure>
<ul>
<li>这个默认值的方式箭头函数也可以使用</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fn = <span class="function">(<span class="params">a = <span class="number">10</span></span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(a);</span><br><span class="line">&#125;;</span><br><span class="line">fn(); <span class="comment">// 不传递参数的时候，函数内部的 a 就是 10</span></span><br><span class="line">fn(<span class="number">20</span>); <span class="comment">// 传递了参数 20 的时候，函数内部的 a 就是 20</span></span><br></pre></td></tr></table></figure>
<ul>
<li>注意： <strong>箭头函数如果你需要使用默认值的话，那么一个参数的时候也需要写 （）</strong></li>
</ul>
</li>
</ul>
<h3 id="解构赋值"><a href="#解构赋值" class="headerlink" title="解构赋值"></a>解构赋值</h3><ul>
<li>解构赋值，就是快速的从对象或者数组中取出成员的一个语法方式</li>
</ul>
<h4 id="解构对象"><a href="#解构对象" class="headerlink" title="解构对象"></a>解构对象</h4><ul>
<li><p>快速的从对象中获取成员</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ES5 的方法向得到对象中的成员</span></span><br><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  name: <span class="string">&quot;Jack&quot;</span>,</span><br><span class="line">  age: <span class="number">18</span>,</span><br><span class="line">  gender: <span class="string">&quot;男&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> name = obj.name;</span><br><span class="line"><span class="keyword">let</span> age = obj.age;</span><br><span class="line"><span class="keyword">let</span> gender = obj.gender;</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 解构赋值的方式从对象中获取成员</span></span><br><span class="line"><span class="keyword">const</span> obj = &#123;</span><br><span class="line">  name: <span class="string">&quot;Jack&quot;</span>,</span><br><span class="line">  age: <span class="number">18</span>,</span><br><span class="line">  gender: <span class="string">&quot;男&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 前面的 &#123;&#125; 表示我要从 obj 这个对象中获取成员了</span></span><br><span class="line"><span class="comment">// name age gender 都得是 obj 中有的成员</span></span><br><span class="line"><span class="comment">// obj 必须是一个对象</span></span><br><span class="line"><span class="keyword">let</span> &#123; name, age, gender &#125; = obj;</span><br></pre></td></tr></table></figure>
<h4 id="解构数组"><a href="#解构数组" class="headerlink" title="解构数组"></a>解构数组</h4></li>
<li><p>快速的从数组中获取成员</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ES5 的方式从数组中获取成员</span></span><br><span class="line"><span class="keyword">const</span> arr = [<span class="string">&quot;Jack&quot;</span>, <span class="string">&quot;Rose&quot;</span>, <span class="string">&quot;Tom&quot;</span>];</span><br><span class="line"><span class="keyword">let</span> a = arr[<span class="number">0</span>];</span><br><span class="line"><span class="keyword">let</span> b = arr[<span class="number">1</span>];</span><br><span class="line"><span class="keyword">let</span> c = arr[<span class="number">2</span>];</span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 使用解构赋值的方式从数组中获取成员</span></span><br><span class="line"><span class="keyword">const</span> arr = [<span class="string">&quot;Jack&quot;</span>, <span class="string">&quot;Rose&quot;</span>, <span class="string">&quot;Tom&quot;</span>];</span><br><span class="line"></span><br><span class="line"><span class="comment">// 前面的 [] 表示要从 arr 这个数组中获取成员了</span></span><br><span class="line"><span class="comment">// a b c 分别对应这数组中的索引 0 1 2</span></span><br><span class="line"><span class="comment">// arr 必须是一个数组</span></span><br><span class="line"><span class="keyword">let</span> [a, b, c] = arr;</span><br></pre></td></tr></table></figure>
<h4 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h4></li>
<li><p><code>&#123;&#125;</code> 是专门解构对象使用的</p>
</li>
<li><p><code>[]</code> 是专门解构数组使用的</p>
</li>
<li><p>不能混用</p>
</li>
</ul>
<h3 id="模版字符串"><a href="#模版字符串" class="headerlink" title="模版字符串"></a>模版字符串</h3><ul>
<li><p>ES5 中我们表示字符串的时候使用 <code>&#39;&#39;</code> 或者 <code>&quot;&quot;</code></p>
</li>
<li><p>在 ES6 中，我们还有一个东西可以表示字符串，就是 **``**（反引号）</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">`hello world`</span>;</span><br><span class="line"><span class="built_in">console</span>.log(<span class="keyword">typeof</span> str); <span class="comment">// string</span></span><br></pre></td></tr></table></figure></li>
<li><p>和单引号好友双引号的区别</p>
<ol>
<li><p>反引号可以换行书写</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 这个单引号或者双引号不能换行，换行就会报错了</span></span><br><span class="line"><span class="keyword">let</span> str = <span class="string">&#x27;hello world&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 下面这个就报错了</span></span><br><span class="line"><span class="keyword">let</span> str2 = <span class="string">&#x27;hello</span></span><br><span class="line"><span class="string">world&#x27;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> str = <span class="string">`</span></span><br><span class="line"><span class="string">	hello</span></span><br><span class="line"><span class="string">	world</span></span><br><span class="line"><span class="string">`</span>;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(str); <span class="comment">// 是可以使用的</span></span><br></pre></td></tr></table></figure></li>
<li><p>反引号可以直接在字符串里面拼接变量</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ES5 需要字符串拼接变量的时候</span></span><br><span class="line"><span class="keyword">let</span> num = <span class="number">100</span>;</span><br><span class="line"><span class="keyword">let</span> str = <span class="string">&quot;hello&quot;</span> + num + <span class="string">&quot;world&quot;</span> + num;</span><br><span class="line"><span class="built_in">console</span>.log(str); <span class="comment">// hello100world100</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 直接写在字符串里面不好使</span></span><br><span class="line"><span class="keyword">let</span> str2 = <span class="string">&quot;hellonumworldnum&quot;</span>;</span><br><span class="line"><span class="built_in">console</span>.log(str2); <span class="comment">// hellonumworldnum</span></span><br></pre></td></tr></table></figure>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 模版字符串拼接变量</span></span><br><span class="line"><span class="keyword">let</span> num = <span class="number">100</span>;</span><br><span class="line"><span class="keyword">let</span> str = <span class="string">`hello<span class="subst">$&#123;num&#125;</span>world<span class="subst">$&#123;num&#125;</span>`</span>;</span><br><span class="line"><span class="built_in">console</span>.log(str); <span class="comment">// hello100world100</span></span><br></pre></td></tr></table></figure>
<ul>
<li>在 <strong>``</strong> 里面的 <code>$&#123;&#125;</code> 就是用来书写变量的位置</li>
</ul>
</li>
</ol>
</li>
</ul>
<h3 id="展开运算符"><a href="#展开运算符" class="headerlink" title="展开运算符"></a>展开运算符</h3><ul>
<li><p>ES6 里面号新添加了一个运算符 <code>...</code> ，叫做展开运算符</p>
</li>
<li><p>作用是把数组展开</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="built_in">console</span>.log(...arr); <span class="comment">// 1 2 3 4 5</span></span><br></pre></td></tr></table></figure></li>
<li><p>合并数组的时候可以使用</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>];</span><br><span class="line"><span class="keyword">let</span> arr2 = [...arr, <span class="number">5</span>];</span><br><span class="line"><span class="built_in">console</span>.log(arr2);</span><br></pre></td></tr></table></figure></li>
<li><p>也可以合并对象使用</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> obj = &#123;</span><br><span class="line">  name: <span class="string">&quot;Jack&quot;</span>,</span><br><span class="line">  age: <span class="number">18</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">let</span> obj2 = &#123;</span><br><span class="line">  ...obj,</span><br><span class="line">  gender: <span class="string">&quot;男&quot;</span>,</span><br><span class="line">&#125;;</span><br><span class="line"><span class="built_in">console</span>.log(obj2);</span><br></pre></td></tr></table></figure></li>
<li><p>在函数传递参数的时候也可以使用</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> arr = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">fn</span>(<span class="params">a, b, c</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(a);</span><br><span class="line">  <span class="built_in">console</span>.log(b);</span><br><span class="line">  <span class="built_in">console</span>.log(c);</span><br><span class="line">&#125;</span><br><span class="line">fn(...arr);</span><br><span class="line"><span class="comment">// 等价于 fn(1, 2, 3)</span></span><br></pre></td></tr></table></figure></li>
</ul>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">娄豪杰</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="http://example.com/article/b77b8aef.html">http://example.com/article/b77b8aef.html</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">HJ BLOG</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/JavaScript/">JavaScript</a></div><div class="post_share"><div class="social-share" data-image="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&amp;refer=http%3A%2F%2Fpic1.zhimg.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1645530798&amp;t=e5c6ac493d1c8b04cd2a4740e1cc9870" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/article/148a70f3.html"><img class="prev-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&amp;refer=http%3A%2F%2Fpic1.zhimg.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1645530798&amp;t=e5c6ac493d1c8b04cd2a4740e1cc9870" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">十五、万物皆对象（面向对象思想）</div></div></a></div><div class="next-post pull-right"><a href="/article/abbe594b.html"><img class="next-cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&amp;refer=http%3A%2F%2Fpic1.zhimg.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1645530798&amp;t=e5c6ac493d1c8b04cd2a4740e1cc9870" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">十三、玩转正则-RegExp</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/article/23cb5f5f.html" title="一、初识JavaScript"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645530798&t=e5c6ac493d1c8b04cd2a4740e1cc9870" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-01</div><div class="title">一、初识JavaScript</div></div></a></div><div><a href="/article/4200b2b0.html" title="三、JS循环结构"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645530798&t=e5c6ac493d1c8b04cd2a4740e1cc9870" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-03</div><div class="title">三、JS循环结构</div></div></a></div><div><a href="/article/2c83c14b.html" title="四、JS函数（上）"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645530798&t=e5c6ac493d1c8b04cd2a4740e1cc9870" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-04</div><div class="title">四、JS函数（上）</div></div></a></div><div><a href="/article/bdf08e07.html" title="二、JS分支结构"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645530798&t=e5c6ac493d1c8b04cd2a4740e1cc9870" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-02</div><div class="title">二、JS分支结构</div></div></a></div><div><a href="/article/dcee35d7.html" title="五、JS函数（下）"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645530798&t=e5c6ac493d1c8b04cd2a4740e1cc9870" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-05</div><div class="title">五、JS函数（下）</div></div></a></div><div><a href="/article/23ec2cd1.html" title="六、JS数组[Array]"><img class="cover" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&refer=http%3A%2F%2Fpic1.zhimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1645530798&t=e5c6ac493d1c8b04cd2a4740e1cc9870" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-03-06</div><div class="title">六、JS数组[Array]</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">娄豪杰</div><div class="author-info__description"></div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">70</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">17</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/louhaojie99"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">人生就像品茗，懂得吃苦，才能回甘啊。</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#ES5-%E5%92%8C-ES6"><span class="toc-number">1.</span> <span class="toc-text">ES5 和 ES6</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#ES5-%E5%A2%9E%E5%8A%A0%E7%9A%84%E6%95%B0%E7%BB%84%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-number">1.1.</span> <span class="toc-text">ES5 增加的数组常用方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95%E4%B9%8B-forEach"><span class="toc-number">1.1.1.</span> <span class="toc-text">数组方法之 forEach</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95%E4%B9%8B-map"><span class="toc-number">1.1.2.</span> <span class="toc-text">数组方法之 map</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95%E4%B9%8B-filter"><span class="toc-number">1.1.3.</span> <span class="toc-text">数组方法之 filter</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95%E4%B9%8B-some"><span class="toc-number">1.1.4.</span> <span class="toc-text">数组方法之 some</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95%E4%B9%8B-every"><span class="toc-number">1.1.5.</span> <span class="toc-text">数组方法之 every</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ES7-%E5%A2%9E%E5%8A%A0%E7%9A%84%E6%95%B0%E7%BB%84%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95"><span class="toc-number">1.2.</span> <span class="toc-text">ES7 增加的数组常用方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95%E4%B9%8B-find"><span class="toc-number">1.2.1.</span> <span class="toc-text">数组方法之 find</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%95%B0%E7%BB%84%E6%96%B9%E6%B3%95%E4%B9%8B-findIndex"><span class="toc-number">1.2.2.</span> <span class="toc-text">数组方法之 findIndex</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JSON-%E6%96%B9%E6%B3%95"><span class="toc-number">1.3.</span> <span class="toc-text">JSON 方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#JSON-%E7%9A%84%E4%B8%A4%E4%B8%AA%E6%96%B9%E6%B3%95%EF%BC%88es5-%E6%96%B0%E5%A2%9E%EF%BC%89"><span class="toc-number">1.4.</span> <span class="toc-text">JSON 的两个方法（es5 新增）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#JSON-parse"><span class="toc-number">1.4.1.</span> <span class="toc-text">JSON.parse</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#JSON-stringify"><span class="toc-number">1.4.2.</span> <span class="toc-text">JSON.stringify</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#this-%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-number">1.5.</span> <span class="toc-text">this 关键字</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#call-%E5%92%8C-apply-%E5%92%8C-bind"><span class="toc-number">1.5.1.</span> <span class="toc-text">call 和 apply 和 bind</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#call"><span class="toc-number">1.5.1.1.</span> <span class="toc-text">call</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#apply"><span class="toc-number">1.5.1.2.</span> <span class="toc-text">apply</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#bind"><span class="toc-number">1.5.1.3.</span> <span class="toc-text">bind</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#ES6-%E6%96%B0%E5%A2%9E%E7%9A%84%E5%86%85%E5%AE%B9"><span class="toc-number">1.6.</span> <span class="toc-text">ES6 新增的内容</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B0%E5%A2%9E%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.6.1.</span> <span class="toc-text">新增数据类型</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B0%E5%A2%9E%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84-set-%E5%92%8C-map"><span class="toc-number">1.6.2.</span> <span class="toc-text">新增数据结构 set 和 map</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#let-%E5%92%8C-const-%E5%85%B3%E9%94%AE%E5%AD%97-l"><span class="toc-number">1.6.3.</span> <span class="toc-text">let 和 const 关键字 l</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0"><span class="toc-number">1.6.4.</span> <span class="toc-text">箭头函数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0%E7%9A%84%E7%89%B9%E6%AE%8A%E6%80%A7"><span class="toc-number">1.6.4.1.</span> <span class="toc-text">箭头函数的特殊性</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%87%BD%E6%95%B0%E4%BC%A0%E9%80%92%E5%8F%82%E6%95%B0%E7%9A%84%E6%97%B6%E5%80%99%E7%9A%84%E9%BB%98%E8%AE%A4%E5%80%BC"><span class="toc-number">1.6.5.</span> <span class="toc-text">函数传递参数的时候的默认值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A7%A3%E6%9E%84%E8%B5%8B%E5%80%BC"><span class="toc-number">1.6.6.</span> <span class="toc-text">解构赋值</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A7%A3%E6%9E%84%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.6.6.1.</span> <span class="toc-text">解构对象</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A7%A3%E6%9E%84%E6%95%B0%E7%BB%84"><span class="toc-number">1.6.6.2.</span> <span class="toc-text">解构数组</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%B3%A8%E6%84%8F"><span class="toc-number">1.6.6.3.</span> <span class="toc-text">注意</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A8%A1%E7%89%88%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">1.6.7.</span> <span class="toc-text">模版字符串</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%95%E5%BC%80%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">1.6.8.</span> <span class="toc-text">展开运算符</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/article/cc11b2b0.html" title="二、JavaScript设计模式【下】"><img src="https://picabstract-preview-ftn.weiyun.com/ftn_pic_abs_v3/983a2ddf4c01d0f319b391469f669eba7a1a82ba077da903e3114d4062c26cdea809b197d9cf02bb077af6272de8e1ed?pictype=scale&amp;from=30113&amp;version=3.3.3.3&amp;uin=851681631&amp;fname=12_js.jpg&amp;size=750" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="二、JavaScript设计模式【下】"/></a><div class="content"><a class="title" href="/article/cc11b2b0.html" title="二、JavaScript设计模式【下】">二、JavaScript设计模式【下】</a><time datetime="2021-09-11T16:00:00.000Z" title="发表于 2021-09-12 00:00:00">2021-09-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/cc00b2b0.html" title="一、JavaScript设计模式【上】"><img src="https://picabstract-preview-ftn.weiyun.com/ftn_pic_abs_v3/983a2ddf4c01d0f319b391469f669eba7a1a82ba077da903e3114d4062c26cdea809b197d9cf02bb077af6272de8e1ed?pictype=scale&amp;from=30113&amp;version=3.3.3.3&amp;uin=851681631&amp;fname=12_js.jpg&amp;size=750" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="一、JavaScript设计模式【上】"/></a><div class="content"><a class="title" href="/article/cc00b2b0.html" title="一、JavaScript设计模式【上】">一、JavaScript设计模式【上】</a><time datetime="2021-09-10T16:00:00.000Z" title="发表于 2021-09-11 00:00:00">2021-09-11</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/vue-sound-code10.html" title="十、Vue源码【内置组件篇】"><img src="https://img2.baidu.com/it/u=4067570870,2963075030&amp;fm=253&amp;fmt=auto&amp;app=120&amp;f=JPEG?w=640&amp;h=360" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="十、Vue源码【内置组件篇】"/></a><div class="content"><a class="title" href="/article/vue-sound-code10.html" title="十、Vue源码【内置组件篇】">十、Vue源码【内置组件篇】</a><time datetime="2021-09-09T16:00:00.000Z" title="发表于 2021-09-10 00:00:00">2021-09-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/vue-sound-code9.html" title="九、Vue源码【指令篇】"><img src="https://img2.baidu.com/it/u=4067570870,2963075030&amp;fm=253&amp;fmt=auto&amp;app=120&amp;f=JPEG?w=640&amp;h=360" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="九、Vue源码【指令篇】"/></a><div class="content"><a class="title" href="/article/vue-sound-code9.html" title="九、Vue源码【指令篇】">九、Vue源码【指令篇】</a><time datetime="2021-09-08T16:00:00.000Z" title="发表于 2021-09-09 00:00:00">2021-09-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/article/vue-sound-code8.html" title="八、Vue源码【过滤器篇】"><img src="https://img2.baidu.com/it/u=4067570870,2963075030&amp;fm=253&amp;fmt=auto&amp;app=120&amp;f=JPEG?w=640&amp;h=360" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="八、Vue源码【过滤器篇】"/></a><div class="content"><a class="title" href="/article/vue-sound-code8.html" title="八、Vue源码【过滤器篇】">八、Vue源码【过滤器篇】</a><time datetime="2021-09-07T16:00:00.000Z" title="发表于 2021-09-08 00:00:00">2021-09-08</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic1.zhimg.com%2Fv2-25182ac6197d798d6d8b22d3b0710cbf_1200x500.jpg&amp;refer=http%3A%2F%2Fpic1.zhimg.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg?sec=1645530798&amp;t=e5c6ac493d1c8b04cd2a4740e1cc9870')"><div id="footer-wrap"><div class="copyright">&copy;2017 - 2022 By 娄豪杰</div><div class="footer_custom_text"><div>嗨，欢迎来到我的博客，希望能帮助到您！</div></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div></div><hr/><div id="local-search-results"></div><span class="search-close-button"><i class="fas fa-times"></i></span></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"></div><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-show-text.min.js" data-mobile="false" data-text="富强,民主,文明,和谐,自由,平等,公正,法治" data-fontsize="15px" data-random="true" async="async"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = [
  'title',
  '#config-diff',
  '#body-wrap',
  '#rightside-config-hide',
  '#rightside-config-show',
  '.js-pjax'
]

if (false) {
  pjaxSelectors.unshift('meta[property="og:image"]', 'meta[property="og:title"]', 'meta[property="og:url"]')
}

var pjax = new Pjax({
  elements: 'a:not([target="_blank"]):not([href="/music/"]):not([href="/no-pjax/"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener toc scroll 
  window.removeEventListener('scroll', window.tocScrollFn)

  typeof preloader === 'object' && preloader.initLoading()
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // Analytics
  if (false) {
    MtaH5.pgv()
  }

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/shizuku.model.json"},"display":{"position":"left","width":150,"height":300},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body></html>